package model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
 * Created by admin
 * <p>
 * Date :2020/10/13
 * <p>
 * Description :
 * <p>
 * Version :1.0
 */
public final class Pocker {
    private static final Logger logger = LoggerFactory.getLogger(Pocker.class);

    private static final int NUMS_IN_GROUP = 13;

    private static final int POCKER_COUNT = PockerCard.PockerType.values().length * NUMS_IN_GROUP;

    private final PockerCard[] pocker = new PockerCard[POCKER_COUNT];

    private int index;

    public Pocker() {
        PockerCard.PockerType[] pockerTypes = PockerCard.PockerType.values();
        int count = 0;
        for (int i = 0; i < pockerTypes.length; i++) {
            for (int j = 1; j <= NUMS_IN_GROUP; j++) {
                pocker[count] = new PockerCard(count, pockerTypes[i], PockerCard.PockerPoint.fromValue(j-1));
                count++;
            }
        }
    }

    public void shuffle(){
        for (int i = 0; i < pocker.length; i++) {
            int random = (int) (i + Math.random() * (pocker.length - i));
            PockerCard temp = pocker[random];
            pocker[random] = pocker[i];
            pocker[i] = temp;
        }
    }

    public boolean isValid(){
        for (int i = 0; i < POCKER_COUNT; i++) {
            boolean exists = false;
            for (int j = 0; j < pocker.length; j++) {
                PockerCard pockerCard = pocker[j];
                if(pockerCard.getIndex() == i){
                    exists = true;
                    break;
                }
            }
            if(!exists){
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString() {
        return Arrays.toString(pocker);
    }

    public PockerCard pop() {
        return pocker[index++];
    }
}
